//
//  OrderProductModel.swift
//
//  Created by eme on 2016/12/19
//  Copyright (c) . All rights reserved.
//

import Foundation
import SwiftyJSON
import ModelProtocol

class OrderProductModel: ModelProtocol {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.
    internal let kOrderProductPdstateKey: String = "pdstate"
    internal let kOrderProductPdidKey: String = "pdid"
    internal let kOrderProductPsfidKey: String = "psfid"
    internal let kOrderProductPsfnameKey: String = "psfname"
    internal let kOrderProductImg1Key: String = "img1"
    internal let kOrderProductBdpriceKey: String = "bdprice"
    internal let kOrderProductSkuidKey: String = "skuid"
    internal let kOrderProductPddesKey: String = "pddes"
    internal let kOrderProductPsfname2Key: String = "psfname2"
    internal let kOrderProductIsshowKey: String = "isshow"
    internal let kOrderProductPdcodeKey: String = "pdcode"
    internal let kOrderProductBridKey: String = "brid"
    internal let kOrderProductAgpriceKey: String = "agprice"
    internal let kOrderProductCartidKey: String = "cartid"
    internal let kOrderProductSalecountKey: String = "salecount"
    internal let kOrderProductPsfid2Key: String = "psfid2"
    internal let kOrderProductShpriceKey: String = "shprice"
    internal let kOrderProductPdnameKey: String = "pdname"
    internal let kOrderProductPfpriceKey: String = "pfprice"
    internal let kOrderProductFarpostageKey: String = "farpostage"
    internal let kOrderProductBdidKey: String = "bdid"
    internal let kOrderProductSpidKey: String = "spid"
    internal let kOrderProductPsvalueKey: String = "psvalue"
    internal let kOrderProductMoqKey: String = "moq"
    internal let kOrderProductPostageKey: String = "postage"
    internal let kOrderProductIscheckKey: String = "ischeck"
    internal let kOrderProductStockKey: String = "stock"
    internal let kOrderProductBdnameKey: String = "bdname"
    internal let kOrderProductPsvalue2Key: String = "psvalue2"
    internal let kOrderProductCartnumberKey: String = "cartnumber"
    
    //    skutotalprice
    //    skunum
    var skutotalprice: Double
    var skunum: Int
    var editnum:Int
    var ordertotalposttag:Double
    var subtotal:Double
    // MARK: 属性
    var pdstate: String
    var pdid: Int
    var psfid: Int
    var psfname: String
    var img1: String
    var bdprice: Int
    var skuid: Int
    var pddes: String
    var psfname2: String
    var isshow: String
    var pdcode: String
    var brid: String
    var agprice: Int
    var cartid: Int
    var salecount: Int
    var psfid2: Int
    var shprice: Double
    var pdname: String
    var pfprice: Int
    var farpostage: Int
    var bdid: String
    var spid: String
    var psvalue: String
    var moq: Int
    var postage: Double
    /// 1 表示选择，0 表示未选中
    var ischeck: String
    var stock: Int
    var bdname: String
    var psvalue2: String
    var cartnumber: Int
    var pdunit:String
    var skupostage:Double
    var message:String = ""
    var istimeby:String
    
    //1 物流包邮 2 快递包邮
    var pdsendtype:String
    var show_pdsendtype:String{
        get {
            if self.pdsendtype == "1" {
                return "物流包邮"
            }else if self.pdsendtype == "2" {
                return "快递包邮"
            }else if self.pdsendtype == "3" {
                return "物流到付"
            }else{
                return "快递到付"
            }
        }
    }
    
    var show_psfname:String{
        get{
            if self.psfname.characters.count > 0 {
                return "\(self.psfname) :\(self.psvalue)"
            }else{
                return "无规格"
            }
        }
    }
    var show_psfname2:String{
        get{
            if self.psfname2.characters.count > 0 {
                return "\(self.psfname2) :\(self.psvalue2)"
            }else{
                return ""
            }
        }
    }
    
    var totalCartNumber:Int{
        get{
            return self.cartnumber * self.moq
        }
    }
    var buyNum:Int{
        get {
            if self.hasSelected {
                return self.cartnumber * self.moq
            }else{
                return 0
            }
        }
    }
    var buyMoney:Double{
        get{
            return Double(self.buyNum) * self.shprice
        }
    }
    /// item 是否选中
    var hasSelected:Bool{
        set(newVal){
            if newVal {
                self.ischeck = "1"
            }else{
                self.ischeck = "0"
            }
        }
        get {
            /// 1 表示选择，0 表示未选中
            return self.ischeck == "1"
        }
    }
    
    
    /// 用于换缓存节点中选中购物的数量
    var  subBuyNum:Int = 0
    /// 用于缓存子节点选中商品的价格
    var subBuyMoney:Double = 0.0
    /// item的子数组下是否都选中
    var subHasSelected:Bool{
        get{
            var isOk = true
            var totalNum:Int = 0
            var totalMoney:Double = 0.0
            for item in self.array {
                if item.hasSelected == false {
                    isOk = false
                }
                /// 缓存子节点的 价格和选中数量
                totalNum = totalNum + item.buyNum
                totalMoney = totalMoney + item.buyMoney
            }
            
            self.subBuyNum = totalNum
            self.subBuyMoney = totalMoney
            return isOk
        }
        set(newVal){
            for item  in self.array {
                item.hasSelected = newVal
            }
        }
    }
    
    
    /// 用于换缓存节点中选中购物的数量
    var  allBuyNum:Int = 0
    /// 用于缓存子节点选中商品的价格
    var allBuyMoney:Double = 0.0
    /// 判断所有的子节点是否都选中
    var allHasSelected:Bool{
        get{
            var isOk = true
            var totalNum:Int = 0
            var totalMoney:Double = 0.0
            for item in self.array {
                if item.subHasSelected == false {
                    isOk = false
                }
                totalNum = totalNum + item.subBuyNum
                totalMoney = totalMoney + item.subBuyMoney
            }
            self.allBuyNum = totalNum
            self.allBuyMoney = totalMoney
            return isOk
        }
        set(newVal){
            for item  in self.array {
                item.subHasSelected = newVal
            }
        }
    }
    
    var array:[OrderProductModel]!
    
    
    // MARK: 实现ModelProtocol 协议， 解析json数据
    required  init?(json: JSON) {
        pdstate = json[kOrderProductPdstateKey].stringValue
        pdid = json[kOrderProductPdidKey].intValue
        psfid = json[kOrderProductPsfidKey].intValue
        psfname = json[kOrderProductPsfnameKey].stringValue
        img1 = json[kOrderProductImg1Key].stringValue
        bdprice = json[kOrderProductBdpriceKey].intValue
        skuid = json[kOrderProductSkuidKey].intValue
        pddes = json[kOrderProductPddesKey].stringValue
        psfname2 = json[kOrderProductPsfname2Key].stringValue
        isshow = json[kOrderProductIsshowKey].stringValue
        pdcode = json[kOrderProductPdcodeKey].stringValue
        brid = json[kOrderProductBridKey].stringValue
        agprice = json[kOrderProductAgpriceKey].intValue
        cartid = json[kOrderProductCartidKey].intValue
        salecount = json[kOrderProductSalecountKey].intValue
        psfid2 = json[kOrderProductPsfid2Key].intValue
        shprice = json[kOrderProductShpriceKey].doubleValue
        pdname = json[kOrderProductPdnameKey].stringValue
        pfprice = json[kOrderProductPfpriceKey].intValue
        farpostage = json[kOrderProductFarpostageKey].intValue
        bdid = json[kOrderProductBdidKey].stringValue
        spid = json[kOrderProductSpidKey].stringValue
        psvalue = json[kOrderProductPsvalueKey].stringValue
        moq = json[kOrderProductMoqKey].intValue
        postage = json[kOrderProductPostageKey].doubleValue
        ischeck = json[kOrderProductIscheckKey].stringValue
        stock = json[kOrderProductStockKey].intValue
        bdname = json[kOrderProductBdnameKey].stringValue
        psvalue2 = json[kOrderProductPsvalue2Key].stringValue
        cartnumber = json[kOrderProductCartnumberKey].intValue
        pdunit = json["pdunit"].stringValue
        skunum = json["skunum"].intValue
        editnum = json["editnum"].intValue
        skutotalprice = json["skutotalprice"].doubleValue
        ordertotalposttag = json["ordertotalposttag"].doubleValue
        subtotal = json["subtotal"].doubleValue
        skupostage = json["skupostage"].doubleValue
        pdsendtype = json["pdsendtype"].stringValue
        istimeby = json["istimeby"].stringValue
        array = []
        if let items = json["array"].array {
            for item in items {
                if let orderProductModel = OrderProductModel(json: item){
                    array?.append(orderProductModel)
                }
            }
        }
        
        
    }
    
    
    /**
     Generates description of the object in the form of a NSDictionary.
     - returns: A Key value pair containing all valid values in the object.
     */
    internal func dictionaryRepresentation() -> [String : AnyObject ] {
        
        var dictionary: [String : AnyObject ] = [ : ]
        
        dictionary.updateValue(pdstate as AnyObject, forKey: kOrderProductPdstateKey)
        
        dictionary.updateValue(pdid as AnyObject, forKey: kOrderProductPdidKey)
        
        dictionary.updateValue(psfid as AnyObject, forKey: kOrderProductPsfidKey)
        
        dictionary.updateValue(psfname as AnyObject, forKey: kOrderProductPsfnameKey)
        
        dictionary.updateValue(img1 as AnyObject, forKey: kOrderProductImg1Key)
        
        dictionary.updateValue(bdprice as AnyObject, forKey: kOrderProductBdpriceKey)
        
        dictionary.updateValue(skuid as AnyObject, forKey: kOrderProductSkuidKey)
        
        dictionary.updateValue(pddes as AnyObject, forKey: kOrderProductPddesKey)
        
        dictionary.updateValue(psfname2 as AnyObject, forKey: kOrderProductPsfname2Key)
        
        dictionary.updateValue(isshow as AnyObject, forKey: kOrderProductIsshowKey)
        
        dictionary.updateValue(pdcode as AnyObject, forKey: kOrderProductPdcodeKey)
        
        dictionary.updateValue(brid as AnyObject, forKey: kOrderProductBridKey)
        
        dictionary.updateValue(agprice as AnyObject, forKey: kOrderProductAgpriceKey)
        
        dictionary.updateValue(cartid as AnyObject, forKey: kOrderProductCartidKey)
        
        dictionary.updateValue(salecount as AnyObject, forKey: kOrderProductSalecountKey)
        
        dictionary.updateValue(psfid2 as AnyObject, forKey: kOrderProductPsfid2Key)
        
        dictionary.updateValue(shprice as AnyObject, forKey: kOrderProductShpriceKey)
        
        dictionary.updateValue(pdname as AnyObject, forKey: kOrderProductPdnameKey)
        
        dictionary.updateValue(pfprice as AnyObject, forKey: kOrderProductPfpriceKey)
        
        dictionary.updateValue(farpostage as AnyObject, forKey: kOrderProductFarpostageKey)
        
        dictionary.updateValue(bdid as AnyObject, forKey: kOrderProductBdidKey)
        
        dictionary.updateValue(spid as AnyObject, forKey: kOrderProductSpidKey)
        
        dictionary.updateValue(psvalue as AnyObject, forKey: kOrderProductPsvalueKey)
        
        dictionary.updateValue(moq as AnyObject, forKey: kOrderProductMoqKey)
        
        dictionary.updateValue(postage as AnyObject, forKey: kOrderProductPostageKey)
        
        dictionary.updateValue(ischeck as AnyObject, forKey: kOrderProductIscheckKey)
        
        dictionary.updateValue(stock as AnyObject, forKey: kOrderProductStockKey)
        
        dictionary.updateValue(bdname as AnyObject, forKey: kOrderProductBdnameKey)
        
        dictionary.updateValue(psvalue2 as AnyObject, forKey: kOrderProductPsvalue2Key)
        
        dictionary.updateValue(cartnumber as AnyObject, forKey: kOrderProductCartnumberKey)
        
        
        return dictionary
    }
    
    
}
